package com.intellagric.service.traceability.Impl.trace_record;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.RecordInvestDao;
import com.intellagric.mapper.TraceRecordInvestMapper;
import com.intellagric.module.traceability.InvestRecordVo;
import com.intellagric.pojo.TraceRecordInvest;
import com.intellagric.pojo.TraceRecordInvestExample;
import com.intellagric.service.traceability.trace_record.InvestRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Author: wl
 * @email: 844406720@qq.com
 * @Date: 2018/11/07
 * @DESC： 投入品记录服务实现
 */
@Service
public class InvestRecordServiceImpl implements InvestRecordService {

    @Autowired
    private TraceRecordInvestMapper traceRecordInvestMapper;

    @Autowired
    private RecordInvestDao recordInvestDao;

    /**
     * 添加投入品记录
     *
     * @param traceRecordInvest
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addInvestRecord(TraceRecordInvest traceRecordInvest) {
        if (traceRecordInvestMapper.insertSelective(traceRecordInvest) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除投入品记录
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteInvestRecord(String id) {
        if (traceRecordInvestMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改投入品记录
     * @param traceRecordInvest
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editInvestRecord(TraceRecordInvest traceRecordInvest) {
        if (traceRecordInvestMapper.updateByPrimaryKeySelective(traceRecordInvest) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询投入品记录信息
     * @param id
     * @return IftDataRecord
     */
    @Override
    public TraceRecordInvest getInvestRecordById(String id) {
        return traceRecordInvestMapper.selectByPrimaryKey(id);    }

    /**
     * 查询投入品记录信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestRecordList() {
        TraceRecordInvestExample traceRecordInvestExample = new TraceRecordInvestExample();
        traceRecordInvestExample.createCriteria().andIdIsNotNull();
        List<TraceRecordInvest> traceRecordInvestList = traceRecordInvestMapper.selectByExample(traceRecordInvestExample);
        return LayuiDataGridResult.success().add(traceRecordInvestList,traceRecordInvestList.size());
    }

    /**
     * 分页查询投入品记录信息列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestRecordPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceRecordInvestExample traceRecordInvestExample = new TraceRecordInvestExample();
        traceRecordInvestExample.createCriteria().andIdIsNotNull();
        List<TraceRecordInvest> traceRecordInvestList = traceRecordInvestMapper.selectByExample(traceRecordInvestExample);
        PageInfo<TraceRecordInvest> pageInfo = new PageInfo<>(traceRecordInvestList);
        return LayuiDataGridResult.success().add(traceRecordInvestList,pageInfo.getTotal());
    }

    /**
     * 多条件分页查询投入品记录信息列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestRecordPageByVo(int page, int rows, InvestRecordVo investRecordVo) {
        PageHelper.startPage(page,rows);
        List<TraceRecordInvest> traceRecordInvestList = recordInvestDao.selectInvestRecordPageByVo(investRecordVo);
        PageInfo<TraceRecordInvest> pageInfo = new PageInfo<>(traceRecordInvestList);
        return LayuiDataGridResult.success().add(traceRecordInvestList,pageInfo.getTotal());
    }
}
